﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using CSP.Model.CSP;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    public class ContactWayBll : CSPCommonBll, IContactWayBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEY = "ContactWay_";

        private IContactWayDao _contactWayDao;

        public ContactWayBll()
        {
            _cacheProvider = CachedProviderFactory.GetMemCachedProvider();
            _contactWayDao = CSPDaoFactory.GetInstance().GetContactWayDao();
        }


        public ICollection<Model.CSP.ContactWay> FindByUser(string userName)
        {
            string cacheKey = CACHEKEY + userName.ToString();
            ICollection<ContactWay> contactWay = _cacheProvider.GetData<Collection<ContactWay>>(cacheKey);
            if (contactWay == null)
            {
                contactWay = _contactWayDao.FindByUser(userName);
                _cacheProvider.AddData(cacheKey, contactWay, DateTime.Now.AddHours(1));
            }

            return contactWay;
        }

        public ContactWay GetDataByIdAndUser(int id, string userName)
        {
            ContactWay contactWay = _contactWayDao.GetDataByIdAndUser(id, userName);
            return contactWay;
        }

        public Model.CSP.ContactWay GetDataById(int id)
        {
            ContactWay contactWay = _contactWayDao.GetDataById(id);
            return contactWay;
        }

        public ICollection<Model.CSP.ContactWay> GeDatatByIds(int[] Ids)
        {
            //To do
            throw new NotImplementedException();
        }

        public void Add(Model.CSP.ContactWay obj)
        {
            string cacheKey = CACHEKEY + obj.username.ToString();
            if (obj == null)
            {
                throw new Exception("obj Can't be null");
            }
            _contactWayDao.Add(obj);

            _cacheProvider.Remove(cacheKey);
        }

        public void Update(Model.CSP.ContactWay obj)
        {
            string cacheKey = CACHEKEY + obj.username.ToString();
            _contactWayDao.Update(obj);

            _cacheProvider.Remove(cacheKey);
        }

        public void DeleteById(int id)
        {
            var authentication = _contactWayDao.DeleteById(id);

            string cacheKey = CACHEKEY + authentication.username.ToString();

            _cacheProvider.Remove(cacheKey);
        }

        public void RefreshCache()
        {
            //To Do
            throw new NotImplementedException();
        }

        public ContactWay DeleteByIdAndUser(int id, string userName)
        {
            var contactWay = _contactWayDao.DeleteByIdAndUser(id, userName);

            string cacheKey = CACHEKEY + contactWay.username.ToString();
            _cacheProvider.Remove(cacheKey);

            return contactWay;
        }


        public ICollection<ContactWay> FindByUsers(List<string> userNames)
        {
            ICollection<ContactWay> contactWays = _contactWayDao.FindByUsers(userNames);
            return contactWays;
        }

        public int GetCountByUser(string userName)
        {
            return _contactWayDao.GetCountByUser(userName);
        }
    }
}
